#include "Map.hpp"
#include <iostream>
#include <fstream>
#include <sstream>

using namespace sf;

//Constructor(s)

Map::Map(RenderWindow &p_app)
{
	window = &p_app;

	if(!tileset.LoadFromFile(TILESET_FILEPATH))
    {
        std::cout << "Error during import" << TILESET_FILEPATH << std::endl;
    }

	config(MAP_FILEPATH);

	plateau = new Tile*[configuration["sizeY"]];
	for(int i = 0; i < configuration["sizeY"]; i++){
		plateau[i] = new Tile[configuration["sizeX"]];
	}

	init(MAP_FILEPATH);
}

Map::Map(RenderWindow &p_app, std::string p_map, std::string p_image)
{
	window = &p_app;

	if(!tileset.LoadFromFile(p_image)) //a remplacer par une image par default
    {
        std::cout << "Error during import" << p_image << std::endl;
    }

	config(p_map);

	plateau = new Tile*[configuration["sizeY"]];
	for(int i = 0; i < configuration["sizeY"]; i++){
		plateau[i] = new Tile[configuration["sizeX"]];
	}

	init(p_map);
}

//Destructor

Map::~Map()
{
	for(int i = 0; i < configuration["sizeY"]; i++){
		delete[] plateau[i];
	}
    delete[] plateau;
}

//Getter(s)

int Map::get_height()
{
	return configuration["sizeY"];
}

int Map::get_width()
{
	return configuration["sizeX"];
}

Tile& Map::get_cell(int p_i, int p_j)
{
	return plateau[p_i][p_j];
}

//Other(s)

void Map::init(std::string p_map)
{
	std::ifstream flux(p_map);
	std::istringstream convert;
	std::string cell;
	int nb;
	
	for(int i = 0; i < 2; i++){
		getline(flux, cell);
	}
	
	if(flux)
	{
		for(int i = 0; i < configuration["sizeY"]; i++)
		{
			for(int j = 0; j < configuration["sizeX"]; j++)
			{
				flux >> cell;
				switch(cell[0])
				{
					case 'c':
						plateau[i][j].set_type(BREAKABLE);
						break;
					case 'i':
						plateau[i][j].set_type(UNBREAKABLE);
						break;
					case 'v':
						plateau[i][j].set_type(BACKGROUND);
						break;
					default:
						plateau[i][j].set_type(BACKGROUND);
						break;
				}

				Vector2f position((float)j*TILE_SIZE.x, (float)i*TILE_SIZE.y);

				cell = cell.substr(1, cell.size() - 1);
				convert.clear();
				convert.str(cell);
				convert >> nb;

				plateau[i][j].initTile(position, tileset, nb);
			}
			getline(flux, cell);
		}
	} else {
		std::cout << "ERREUR: Impossible d'ouvrir le fichier en lecture." << std::endl;
		/* afficher dans la fenetre d'un erreur de chargement de la map */
	}
	/* Reflechir si on met 1 fichier txt par map ou plusieurs map dans un seul fichier */
	flux.close();
}

void Map::affiche()
{
	for(int i = 0; i < configuration["sizeY"]; i++)
	{
		for(int j = 0; j < configuration["sizeX"]; j++)
		{
			window->Draw(plateau[i][j].get_tileSprite());
		}
	}
}

void Map::config(std::string conf_file)
{
	std::ifstream flux(conf_file);
	std::istringstream convert;
	std::string word;

	flux >> word;
	flux >> word;

	convert.str(word);
	convert >> configuration["sizeX"];

	flux >> word;
	flux >> word;

	convert.clear();
	convert.str(word);
	convert >> configuration["sizeY"];

	flux.close();
}
